package com.lenovo.agricultureclient.ui.activity;

import com.lenovo.agricultureclient.R;
import com.lenovo.agricultureclient.request.BaseRequest;
import com.lenovo.agricultureclient.request.LoginRequest;
import com.lenovo.agricultureclient.request.BaseRequest.RequestResult;
import com.lenovo.agricultureclient.ui.dialog.FindPwdDialog;
import com.lenovo.agricultureclient.ui.dialog.UserRegisterDialog;

import android.app.Dialog;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
/**
 * 用户登录界面，其中还包含了设置Ip的对话框界面
 * @author zongbingwu
 *
 */
public class UserLoginActivity extends AppBaseActivity 
{
	private static final String TAG = "UserLoginActivity";
	
	//设置IP的文本款
	private TextView mSetIPTV;
	//注册的文本款
	private TextView mRegisterTV;
	//找回密码的文本款
	private TextView mFindPwdTV;
	
	//用户名
	private EditText mUsernameET;
	//密码
	private EditText mPasswordET;
	//登录按钮
	private Button mLoginBtn;
	
	//用户登录请求对象
	private LoginRequest mLoginRequest;
	//记录是否已经运行用户引导界面
	private boolean isRunStudyScreen = false;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.user_login);
		
		if(mApp.isFirstRun() && !isRunStudyScreen){
			//如果app时第一次运行，则先显示用户引导界面
			isRunStudyScreen = true;
			Intent intent = new Intent(this, StudyActivity.class);
			startActivity(intent);
			//finish();
		}
		
		initView();
		//创建用户登录请求对象
		mLoginRequest = new LoginRequest();
		mLoginRequest.setOnResponseEventListener(new BaseRequest.OnResponseEventListener() {
			@Override
			public void onResponse(BaseRequest request, RequestResult result) {
				//处理用户登录请求的结果
				dismissLoadDialog();
				if(mLoginRequest.isSuccess()){
					//然后进入主界面
					Intent intent = new Intent(UserLoginActivity.this, MainActivity.class);
					startActivity(intent);
					finish();
				} else {
					//登录失败则提示用户
					showAlertDialog(getString(R.string.prompt), getString(R.string.login_failed));
				}
			}
		});
	}
	
	private void initView()
	{
		mSetIPTV = (TextView)findViewById(R.id.set_ip_text_view);
		mSetIPTV.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				//显示设置IP的对话框
				showSetIpDialog();
			}
		});
		
		mRegisterTV = (TextView)findViewById(R.id.register_text_view);
		mRegisterTV.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				//显示注册对话框
				UserRegisterDialog dialog = new UserRegisterDialog(UserLoginActivity.this,mApp);
				dialog.show();
			}
		});
		
		mFindPwdTV = (TextView)findViewById(R.id.forget_pwd_text_view);
		mFindPwdTV.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				//显示找回密码对话框
				FindPwdDialog dialog = new FindPwdDialog(UserLoginActivity.this,mApp);
				dialog.show();
			}
		});
		
		mUsernameET = (EditText)findViewById(R.id.account_edit_text);
		mUsernameET.setText(mApp.getUsername());
		mPasswordET = (EditText)findViewById(R.id.password_edit_text);
		mPasswordET.setText(mApp.getPassword());
		mLoginBtn = (Button)findViewById(R.id.login_button);
		mLoginBtn.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				//开始登录
				userLoginEntry();
			}
		});
	}
	
	//开始登录
	private void userLoginEntry()
	{
		String username = mUsernameET.getText().toString().trim();
    	String password = mPasswordET.getText().toString().trim();
    	
    	//先检查用户名和密码
    	if(username.equals("")){
    		showAlertDialog(getString(R.string.prompt), getString(R.string.username_not_empty));
    		return;
    	}
    	if(password.equals("")){
    		showAlertDialog(getString(R.string.prompt), getString(R.string.password_not_empty));
    		return;
    	}
    	//先保存用户名和密码
		mApp.setUsername(username);
		mApp.setPassword(password);
    	//将用户名和密码封装到请求中
    	mLoginRequest.setUsername(username);
    	mLoginRequest.setPassword(password);
    	//开始向服务器端发送请求
    	startRequest(mLoginRequest);
    	//显示等待框
    	showLoadDialog();
	}
	
	//show setting IP address dialog
	private void showSetIpDialog()
	{
		final Dialog myDialog = new Dialog(this);//创建一个对话框
		myDialog.show();
		myDialog.getWindow().setBackgroundDrawable(new ColorDrawable(0));  //设置背景颜色
		//设置对话框的界面布局
		Window win = myDialog.getWindow(); 
		win.setContentView(R.layout.set_ip_dialog);
		//设置关闭按钮
		win.findViewById(R.id.close).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				myDialog.dismiss();
			}
		});
		
		final EditText ip1ET = (EditText)win.findViewById(R.id.ip1_edit_text);
		final EditText ip2ET = (EditText)win.findViewById(R.id.ip2_edit_text);
		final EditText ip3ET = (EditText)win.findViewById(R.id.ip3_edit_text);
		final EditText ip4ET = (EditText)win.findViewById(R.id.ip4_edit_text);
		//显示上次设置的ip地址
		String ipStr[] = mApp.getServerIpStr().split("\\.");
		if(ipStr.length>=4)
		{
			ip1ET.setText(ipStr[0]);
			ip2ET.setText(ipStr[1]);
			ip3ET.setText(ipStr[2]);
			ip4ET.setText(ipStr[3]);
		}
		
		win.findViewById(R.id.ok_button).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				//点击OK以后，设置IP地址
				try 
				{
					int ip1,ip2,ip3,ip4;
					ip1 = Integer.parseInt(ip1ET.getText().toString().trim());
					ip2 = Integer.parseInt(ip2ET.getText().toString().trim());
					ip3 = Integer.parseInt(ip3ET.getText().toString().trim());
					ip4 = Integer.parseInt(ip4ET.getText().toString().trim());
					String ipStr =ip1+"."+ip2+"."+ip3+"."+ip4;
					//保存新的IP地址
					mApp.setServerIpStr(ipStr);
					Log.d(TAG,"set server IP:"+ipStr);
					myDialog.dismiss();
				} catch (Exception e) {
					showAlertDialog(getString(R.string.prompt), getString(R.string.invalid_ip));
				}
			}
		});
	}

}
